package com.geo.auth.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.Builder;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 角色实体类
 * 
 * @author GEO Team
 * @version 1.0
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName("sys_role")
public class Role {
    
    /**
     * 角色ID
     */
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private String id;
    
    /**
     * 角色编码（唯一）
     */
    @NotBlank(message = "角色编码不能为空")
    @Size(max = 50, message = "角色编码长度不能超过50个字符")
    @TableField("role_code")
    private String roleCode;
    
    /**
     * 角色名称
     */
    @NotBlank(message = "角色名称不能为空")
    @Size(max = 100, message = "角色名称长度不能超过100个字符")
    @TableField("role_name")
    private String roleName;
    
    /**
     * 角色描述
     */
    @TableField("description")
    private String description;
    
    /**
     * 角色状态
     */
    @TableField("status")
    private RoleStatus status;
    
    /**
     * 角色类型
     */
    @TableField("role_type")
    private RoleType roleType;
    
    /**
     * 排序
     */
    @TableField("sort_order")
    private Integer sortOrder;
    
    /**
     * 是否为默认角色
     */
    @TableField("is_default")
    private Boolean isDefault;
    
    /**
     * 数据权限范围
     */
    @TableField("data_scope")
    private DataScope dataScope;
    
    /**
     * 备注
     */
    @TableField("remark")
    private String remark;
    
    /**
     * 创建时间
     */
    @TableField(value = "created_at", fill = FieldFill.INSERT)
    private LocalDateTime createdAt;
    
    /**
     * 更新时间
     */
    @TableField(value = "updated_at", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedAt;
    
    /**
     * 创建者ID
     */
    @TableField("created_by")
    private String createdBy;
    
    /**
     * 更新者ID
     */
    @TableField("updated_by")
    private String updatedBy;
    
    /**
     * 逻辑删除标志
     */
    @TableLogic
    @TableField("deleted")
    private Integer deleted;
    
    /**
     * 角色状态枚举
     */
    public enum RoleStatus {
        ACTIVE(1, "启用"),
        INACTIVE(0, "禁用");
        
        private final Integer code;
        private final String description;
        
        RoleStatus(Integer code, String description) {
            this.code = code;
            this.description = description;
        }
        
        public Integer getCode() {
            return code;
        }
        
        public String getDescription() {
            return description;
        }
        
        public static RoleStatus fromCode(Integer code) {
            for (RoleStatus status : values()) {
                if (status.code.equals(code)) {
                    return status;
                }
            }
            return INACTIVE;
        }
    }
    
    /**
     * 角色类型枚举
     */
    public enum RoleType {
        SYSTEM("system", "系统角色"),
        BUSINESS("business", "业务角色"),
        CUSTOM("custom", "自定义角色");
        
        private final String code;
        private final String description;
        
        RoleType(String code, String description) {
            this.code = code;
            this.description = description;
        }
        
        public String getCode() {
            return code;
        }
        
        public String getDescription() {
            return description;
        }
        
        public static RoleType fromCode(String code) {
            for (RoleType type : values()) {
                if (type.code.equals(code)) {
                    return type;
                }
            }
            return CUSTOM;
        }
    }
    
    /**
     * 数据权限范围枚举
     */
    public enum DataScope {
        ALL("1", "全部数据权限"),
        CUSTOM("2", "自定义数据权限"),
        DEPT("3", "本部门数据权限"),
        DEPT_AND_CHILD("4", "本部门及以下数据权限"),
        SELF("5", "仅本人数据权限");
        
        private final String code;
        private final String description;
        
        DataScope(String code, String description) {
            this.code = code;
            this.description = description;
        }
        
        public String getCode() {
            return code;
        }
        
        public String getDescription() {
            return description;
        }
        
        public static DataScope fromCode(String code) {
            for (DataScope scope : values()) {
                if (scope.code.equals(code)) {
                    return scope;
                }
            }
            return SELF;
        }
    }
    
    /**
     * 检查角色是否可用
     * @return 是否可用
     */
    public boolean isAvailable() {
        return status == RoleStatus.ACTIVE;
    }
    
    /**
     * 检查是否为系统角色
     * @return 是否为系统角色
     */
    public boolean isSystemRole() {
        return roleType == RoleType.SYSTEM;
    }
    
    /**
     * 检查是否为默认角色
     * @return 是否为默认角色
     */
    public boolean isDefaultRole() {
        return Boolean.TRUE.equals(isDefault);
    }
    
    // 手动添加缺失的getter方法
    public String getRoleCode() {
        return roleCode;
    }
    
    public String getId() {
        return id;
    }
    
    public String getRoleName() {
        return roleName;
    }
}